package com.example.courseapp.adapter;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.courseapp.R;
import com.example.courseapp.RetrofitHelper;
import com.example.courseapp.activity.ChildInfoActivity;
import com.example.courseapp.bean.ChildInformation;
import com.example.courseapp.net.CourseService;

import java.util.ArrayList;
import java.util.List;

public class ChildAdapter extends RecyclerView.Adapter<ChildAdapter.ViewHolder> {

    List<ChildInformation> courseList = new ArrayList<>();

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.course_item, parent, false);
        return new ViewHolder(view);
    }

    CourseService service = RetrofitHelper.getInstance().createService(CourseService.class);

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        ChildInformation course = courseList.get(position);
        holder.name.setText("姓名："+course.getChildName()+"");
        holder.sex.setText("性别："+course.getSex()+"");
        holder.age.setText("年龄："+course.getAge()+"");
        holder.weight.setText("体重："+course.getWeight()+"");

        holder.button.setOnClickListener(v -> {
            Intent intent = new Intent(holder.itemView.getContext(), ChildInfoActivity.class);
            intent.putExtra("childID",course.getChildID());

            holder.itemView.getContext().startActivity(intent);
        });

    }

    @Override
    public int getItemCount() {
        return courseList.size();
    }

    @SuppressLint("NotifyDataSetChanged")
    public void bindData(List<ChildInformation> courseList) {
        this.courseList.clear();
        this.courseList.addAll(courseList);
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView name, age, weight,sex;
        Button button;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.name);
            age = itemView.findViewById(R.id.age);
            sex = itemView.findViewById(R.id.sex);
            weight = itemView.findViewById(R.id.weight);
            button = itemView.findViewById(R.id.button);
        }
    }
}
